import AMapLoader from '@amap/amap-jsapi-loader';
import mapCarIcon from '@/assets/map/m_0_10_0_1.png';
let curAMap = null,
  cueMap = null;
//进行地图初始化
function initMap() {
  return AMapLoader.load({
    key: 'c5426c6c4c8dadb23c1e8abeecbd52c8', // 申请好的Web端开发者Key，首次调用 load 时必填
    version: '1.4.15', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  })
    .then((AMap) => {
      const map = new AMap.Map('container', {
        //设置地图容器id
        viewMode: '3D', //是否为3D地图模式
        zoom: 18, //初始化地图级别
        zooms: [3, 20],
        center: [109.514625, 24.368923], //初始化地图中心点位置
        expandZoomRange: true,
      });
      curAMap = AMap;
      cueMap = map;
      map.setMapStyle('amap://styles/darkblue');
    })
    .catch((e) => {
      console.log(e);
    });
}
let curMarker = null;
// 添加点标记
export const addMarker = (position, angle, AMap = curAMap, map = cueMap) => {
  if (!AMap || !map) {
    return;
  }
  console.log('--', position, angle, new AMap.Size(20, 56));
  const icon = new AMap.Icon({
    // 图标尺寸
    size: new AMap.Size(20, 56),
    // 图标的取图地址
    image: mapCarIcon,
    // 图标所用图片大小
    imageSize: new AMap.Size(20, 56),
  });
  curMarker = new AMap.Marker({
    icon,
    position,
    angle,
    // offset: new AMap.Pixel(-20, -56), // 设置点标记偏移量
  });
  curMarker.setMap(map);
};
// 设置点标记位置
export function setMarker(position, angle, marker = curMarker) {
  if (!marker) return;
  // panTo;
  // cueMap.panTo(position);
  // cueMap.setCenter(position);
  marker.setPosition(position);
  marker.setAngle(angle);
}

// 定位功能
export const geolocation = () => {};
// 销毁地图功能
export const mapDestroy = (map = cueMap) => {
  map && map.destroy();
};

export default initMap;
